package gdconf

import (
	"server_cluster/common/logger"
)

// 武器等级配置表

type WeaponLevelConfig struct {
	Id               int32            `csv:"Id" gorm:"primary_key;autoIncrement:false"` // 武器等级id
	LevelGroup       int32            `csv:"LevelGroup,omitempty"`                      // 升级组
	Level            int32            `csv:"Level,omitempty"`                           // 等级
	Exp              int32            `csv:"Exp,omitempty"`                             // 升到下一等级所需经验
	FightPropAddList FightPropAddList `csv:"Attribute1,omitempty"`                      // 战斗属性列表
}

func (w *WeaponLevelConfig) TableName() string {
	return "config_weapon_level"
}

func (g *GameDataConfig) saveWeaponLevelConfig() {
	saveTableToDb[WeaponLevelConfig](g.Db, readTable[WeaponLevelConfig](g.CsvPathPrefix+"Weaponlevel.csv"))
}

func (g *GameDataConfig) loadWeaponLevelConfig() {
	g.GameDataMaps.WeaponLevelConfigMap = make(map[int32]map[int32]*WeaponLevelConfig)
	for _, weaponLevelConfig := range loadTableFromDb[WeaponLevelConfig](g.Db) {
		_, exist := g.GameDataMaps.WeaponLevelConfigMap[weaponLevelConfig.LevelGroup]
		if !exist {
			g.GameDataMaps.WeaponLevelConfigMap[weaponLevelConfig.LevelGroup] = make(map[int32]*WeaponLevelConfig)
		}
		g.GameDataMaps.WeaponLevelConfigMap[weaponLevelConfig.LevelGroup][weaponLevelConfig.Level] = weaponLevelConfig
	}
	logger.Info("WeaponLevelConfig Count: %v", len(g.GameDataMaps.WeaponLevelConfigMap))
}

func GetWeaponLevelConfigByGroupAndLevel(group int32, level int32) *WeaponLevelConfig {
	value1, exist := CONF.GameDataMaps.WeaponLevelConfigMap[group]
	if !exist {
		logger.Error("[ConfigNotFound] WeaponLevelConfig, group: %v, level: %v", group, level)
		return nil
	}
	value2, exist := value1[level]
	if !exist {
		logger.Error("[ConfigNotFound] WeaponLevelConfig, group: %v, level: %v", group, level)
		return nil
	}
	return value2
}

func GetWeaponLevelConfigMap() map[int32]map[int32]*WeaponLevelConfig {
	return CONF.GameDataMaps.WeaponLevelConfigMap
}
